﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Connectors;
using System.Data;

namespace Marbles.Web.Admin.Content
{
    public partial class ModuleManager : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {             
            Command cmd = new Command("SELECT * FROM modules ORDER BY title ASC");
            DataConnection dbc = new DataConnection();
            cGrdModules.DataSource = dbc.FetchDataTable(cmd);
            cGrdModules.DataBind();
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {
                case "DeleteModule":
                    Core.Modules.ModuleInstallationManager.UnInstall(e.CommandArgument.ToString(), this.Page);
                    LoadData();
                    break;
                case "InstallModule":
                    if (string.IsNullOrEmpty(cTxtFolderName.Text)) { return; }
                    if (!cFileModule.HasFile) { return; }
                    if (!(System.IO.Path.GetExtension(cFileModule.FileName).ToLower() == ".zip")) { return; }

                    Core.Modules.ModuleInstallationManager.Install(cFileModule.FileContent, cTxtFolderName.Text, this.Page);
                    cTxtFolderName.Text = "";
                    LoadData();
                    break;
            }
        }
    }
}